package com.yidu.king.controller;

import com.yidu.king.controller.vo.PersonFeiziVo;
import com.yidu.king.domain.Person;
import com.yidu.king.domain.PersonFeizi;
import com.yidu.king.service.PersonFeiziService;
import com.yidu.king.service.PersonService;
import com.yidu.king.utils.BaseMessage;
import com.yidu.king.utils.Tools;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>@Description: 妃子Controller</p>
 * <p>@Author yidu-liandyao</p>
 * <p>@Date: 2021-01-13</p>
 * <p>@Time: 16:01</p>
 */
@Controller
@RequestMapping("/feizi")
public class PersonFeiziController {
    @Resource
    PersonFeiziService feiziService;

    @RequestMapping("/findAll")
    @ResponseBody
    public Map<String,Object> selectAll(PersonFeiziVo person){
        List<PersonFeiziVo> list = feiziService.selectAll(person);
        int count = feiziService.selectCount(person);
        return Tools.getLayUiMap(list,count);
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public BaseMessage delete(Integer id){
        return feiziService.deleteByPrimaryKey(id)>0?
                BaseMessage.succes():BaseMessage.fail();
    }

    @RequestMapping("/addOrUpdate")
    @ResponseBody
    public BaseMessage addOrUpdate(
            @RequestParam(value = "photo_",required = false )
                    MultipartFile file,
            PersonFeiziVo vo, HttpServletRequest request){

        //处理照片

        //获取上传的文件名
        String fileName = file.getOriginalFilename();
        if(!StringUtils.isEmpty(fileName)) {
            //在springMVC中如何取到request,response,session
            int index = fileName.lastIndexOf('.');
            String hz = fileName.substring(index);

            //使用jdk提供的随机字符串
            String newFileName = UUID.randomUUID().toString() + hz;
            //真实路径
            String realPath = request.getServletContext().getRealPath("/upload");
            File target = new File(realPath + "/" + newFileName);
            try {
                //保存文件
                file.transferTo(target);
            } catch (IOException e) {
                e.printStackTrace();
            }
            vo.setPhoto("/upload/" + newFileName);
        }
        int rows = 0 ;
        if(vo.getId()==null){
            rows = feiziService.insertSelective(vo);
        }else {
            rows = feiziService.updateByPrimaryKeySelective(vo);
        }
        return  rows>0?BaseMessage.succes():BaseMessage.fail();
    }

    /**
     * 显示修改,上屏
     * @param id
     * @return
     */
    @RequestMapping("/showUpdate")
    @ResponseBody
    public BaseMessage showUpdate(Integer id){
        PersonFeiziVo p = feiziService.selectByPrimaryKey(id);
        return BaseMessage.succes(p);
    }
}
